<?php
/*
 * IKPHP 爱客开源社区 @copyright (c) 2012-3000 IKPHP All Rights Reserved 
 * @author 小麦
 * @Email:ikphp@sina.cn
 * @公共抓取html控制器 2014年9月9日18:21:56
 */
namespace IKPHP\Apps\Home\Controllers;

use IKPHP\Common\Controllers\FrontendController;

class ParseController extends FrontendController
{
	private $userid = 0;
	
    public function initialize()
    {
        parent::initialize();
        // 访问者控制
		if ($this->visitor){
			$this->userid = $this->visitor['userid'];
		}  
    }
    
    //抓取新闻
    public function parseurlAction()
    {
    	$link = $this->_post('url');
    	//输入检查
		if(empty($link)){
			$jsonData = array('r'=>1,'html'=>'要抓取的网站链接不能为空');
			return $this->ajaxReturn($jsonData);
		}
		
		//不规则自动加上http
		if(!preg_match('/^http:\/\//i', $link)){
			$link = 'http://'.$link;
		}
		
		//查看远程链接是否有效

		//开始抓取
		require COM_PATH.'Functions/RobotFun.php';
		$data = getUrlFile($link);

		if(empty($data['title'])){
			$jsonData = array('r'=>1,'html'=>'要抓取网站内容失败');
			return $this->ajaxReturn($jsonData);		
		}

		$remoteData['lead_image_url'] = $data['lead_image_url'];
		$remoteData['title'] = $data['title'];
		$remoteData['content'] = strip_tags($data['content']);
		$jsonData = array('r'=>0,'data'=>$remoteData);
		return $this->ajaxReturn($jsonData);   
    }       
}